import axios from 'axios';

// axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';


// 登录请求方法
const loginreq = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        data: params,
        traditional: true,
        transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => res.data);
};

export function postJSON(url, params) {
  return new Promise(((resolve, reject) => {
    axios.post(url,params).then(res => {
      resolve(res); // 正常操作
    }).catch(err => {
      reject(err); // 拒绝操作
    })
  }))
}
// 超级管理员登录请求方法
export const adminLoginReq = (method, url, params) => {
  return axios({
    method: method,
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: JSON.stringify(params),
    traditional: true,
    transformRequest: [
      function(data) {
        let ret = ''
        for (let it in data) {
          ret +=
            encodeURIComponent(it) +
            '=' +
            encodeURIComponent(data[it]) +
            '&'
        }
        return ret
      }
    ]
  }).then(res => res.data);
};
// 通用公用方法
const req = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
             token: localStorage.getItem('logintoken')
        },
        data: params,
        traditional: true,
        transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => res.data);
};

export function  handleExport(url, params) {
  return axios({
    method: "post",
    url: url,
    responseType: 'blob',
    headers: {
      'Content-Type': 'application/json',
      token: localStorage.getItem('logintoken')
    },
    data: params,
  }).then(res => res.data);
};
export {
    loginreq,
    req
}
